package com.ys.controller.web.finance;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ys.service.finance.TcwWanliSupplementaryService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import com.ys.config.MenuId;
import com.ys.dto.SubGridConfigDto;
import com.ys.entity.finance.TcwWanliSupplementary;
import com.ys.entity.finance.TcwWanliSupplementaryItem;
import com.ys.service.sys.CommonApiService;
import com.ys.vo.W;

/**
 * 万里汇账号资金
 */
@Controller
@RequestMapping("/web/finance/tcwWanliSupplementary")
public class WebTcwWanliSupplementaryController {

	@Autowired
	private CommonApiService<TcwWanliSupplementary> commonApiService;

    @Autowired
    private TcwWanliSupplementaryService tcwWanliSupplementaryService;

    /*
     * 新增
     */
    @GetMapping("/toAdd")
    public String toAdd(Model model){
        List<Map<String, Object>> sonlist = new ArrayList<>();

        // 子列表配置信息
        SubGridConfigDto subGridConfig = commonApiService.getSubGrid(MenuId.TCW_WanliSupplementary, sonlist);
        try {
            String bid = commonApiService.getCommonNextBid(MenuId.TCW_WanliSupplementary);

            TcwWanliSupplementary entity = new TcwWanliSupplementary()
                    .setBid(bid)
                    .setCreateTime(new Date())
                    .setEffectStock(1);

            return commonApiService.toAdd(model, entity, MenuId.TCW_WanliSupplementary, null, subGridConfig);


        } catch (Exception e) {
            e.printStackTrace();
            return W.error(model, e.getMessage());
        }
    }

    /*
     * 审核
     */
    @RequiresPermissions("tcwWanliSupplementary:add")
    @GetMapping("/toFlow")
    public String toFlow(Model model, String bid) throws Exception{
        if (bid == null || bid.length() == 0) {
            return W.error(model, "单号不能为空");
        }

        TcwWanliSupplementary entity = new TcwWanliSupplementary().setBid(bid).queryFirst();

        Map<String, Object> extraParam = new HashMap<String, Object>();

        // 子列表配置信息
        List<Map<String, Object>> sonlist = new TcwWanliSupplementaryItem()
                .queryMap();
        SubGridConfigDto subGridConfig = commonApiService.getSubGrid(MenuId.TCW_WanliSupplementary, sonlist);
        return commonApiService.toEdit(model, entity.getId(), new TcwWanliSupplementary(), MenuId.TCW_WanliSupplementary, extraParam, subGridConfig);
    }
    /*
     * 编辑
     */
    @GetMapping("/toEdit")
    public String toEdit(Model model, String bid) throws Exception{
        if (bid == null){
            return W.error(model, "单号不能为空");
        }

        Map<String, Object> extraParam = tcwWanliSupplementaryService.getExtraParam(bid);
        if (extraParam == null){
            return W.error(model, "补账单不存在!");
        }
        List<Map<String, Object>> sonlist = tcwWanliSupplementaryService.getItemList(bid);

        // 子列表配置信息
        SubGridConfigDto subGridConfig = commonApiService.getSubGrid(MenuId.TCW_WanliSupplementary, sonlist);

        return commonApiService.toEdit(model, Long.valueOf(extraParam.get("id").toString()), new TcwWanliSupplementary(), MenuId.TCW_WanliSupplementary, extraParam, subGridConfig);
    }
}